/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socof.wget;

import java.net.MalformedURLException;
import java.net.URL;

/**
 *
 * @author Carlos
 */
public class WebResource 
{
    private static String basePath;
    private static int maxLevel = -1;
        
    //private int id;
    private URL sourceUrl;
    private String localPath;
    private String domain;
    //private WebResourceType type;
    /*private String domain;
    private String filename;
    private String localPath;
    private FileStatus status;    
    private String type;*/
    //private int level;
    //private Date lastUpdated;
    
    public WebResource (String url) throws MalformedURLException
    {
        this.sourceUrl = new URL(url);
        this.setDomain();
    }
    
    public WebResource (String url, String localPath) throws MalformedURLException
    {
        this.sourceUrl = new URL(url);
        this.localPath = localPath;
        this.setDomain();
    }
    
    private void setDomain()
    {
        String host = sourceUrl.getHost();
        this.domain = host.substring(host.indexOf(".") + 1);
    }
    
    public String getDomain()
    {
        return this.domain;
    }
    
    public static int getMaxLevel()
    {
        return maxLevel;
    }

    public static void setMaxLevel(int aMaxLevel)
    {
        maxLevel = aMaxLevel;
    }
    
    public static void setBasePath (String path)
    {
        basePath = path;
    }   
    
    public static String getBasePath ()
    {
        return basePath;
    } 
    
    public static WebResource getNextToDownload()
    {
        // go to db and get next item to be downloaded
        return PendingQueue.getInstance().poll();
    }
    
    public static WebResource getNextToParse()
    {
        // go to db and get next item to be parsed
        return ToParseQueue.getInstance().poll();
    }
    
    public void addToPendingQueue ()
    {
        // saves file to db with status pending
        //this.status = FileStatus.PENDING;
        PendingQueue.getInstance().offer(this);
    }
    
    public void addToParsingQueue ()
    {
        // saves file to db with status pending
        //this.status = FileStatus.TO_PARSE;
        ToParseQueue.getInstance().offer(this);
    }
    
    public boolean download ()
    {
        
        // Download the file using URL to correct location
        
        // if (html) this.setStatusToParse();
        // else this.setStatusComplete();
        
        return true;
    }

    /**
     * @return the sourceUrl
     */
    public URL getSourceUrl()
    {
        return sourceUrl;
    }

    /**
     * @return the localPath
     */
    public String getLocalPath()
    {
        return localPath;
    }

    /**
     * @param localPath the localPath to set
     */
    public void setLocalPath(String localPath)
    {
        this.localPath = localPath;
    }
    
    /*public void setType()
    {
    }
    
    public enum WebResourceType
    {
        HTML,
        OTHER
    }*/
}
